Grokking Oracle Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Oracle Easy
Count Number of Pairs With Absolute Difference K (easy)
Solution: Count Number of Pairs With Absolute Difference K
Degree of an Array (easy)
Solution: Degree of an Array
Find Subsequence of Length K With the Largest Sum (easy)
Solution: Find Subsequence of Length K With the Largest Sum
Rectangle Overlap (easy)
Solution: Rectangle Overlap
Remove All Adjacent Duplicates In String (easy)
Solution: Remove All Adjacent Duplicates In String
Island Perimeter (easy)
Solution: Island Perimeter
Climbing Stairs (easy)
Solution: Climbing Stairs
Move Zeroes (easy)
Solution: Move Zeroes
Excel Sheet Column Title (easy)
Solution: Excel Sheet Column Title
Valid Parentheses (Easy)
Solution: Valid Parentheses
Oracle Medium
Longest Palindromic Substring (medium)
Solution: Longest Palindromic Substring
Single Element in a Sorted Array (medium)
Solution: Single Element in a Sorted Array
Pairs of Songs With Total Durations Divisible by 60 (medium)
Solution: Pairs of Songs With Total Durations Divisible by 60
Search a 2D Matrix II (medium)
Solution: Search a 2D Matrix II
Minimize Maximum of Array (medium)
Solution: Minimize Maximum of Array
Asteroid Collision (medium)
Solution: Asteroid Collision
Combination Sum (medium)
Solution: Combination Sum
Binary Tree Right Side View (medium)
Solution: Binary Tree Right Side View
Sort Colors (medium)
Solution: Sort Colors
Group Anagrams (Medium)
Solution: Group Anagrams
Decode String (Medium)
Solution: Decode String
Number of Islands (medium)
Solution: Number of Islands
Binary Tree Zigzag Level Order Traversal (medium)
Solution: Binary Tree Zigzag Level Order Traversal
Longest Common Subsequence (medium)
Solution: Longest Common Subsequence
Rotting Oranges (medium)
Solution: Rotting Oranges
Meeting Rooms II (medium)
Solution: Meeting Rooms II
Delete Node in a BST (medium)
Solution: Delete Node in a BST
Remove Duplicate Letters (medium)
Solution: Remove Duplicate Letters
Longest Substring Without Repeating Characters (medium)
Solution: Longest Substring Without Repeating Characters (medium)
Search in Rotated Array (medium)
Solution: Search in Rotated Array
Spiral Matrix (medium)
Solution: Spiral Matrix
Container With Most Water (Medium)
Solution: Container With Most Water
3Sum (medium)
Solution: 3Sum
Simplify Path (medium)
Solution: Simplify Path
Next Permutation (medium)
Solution: Next Permutation
Basic Calculator II (medium)
Solution: Basic Calculator II
Merge Intervals (medium)
Solution: Merge Intervals
Restore IP Addresses (medium)
Solution: Restore IP Addresses
Longest Increasing Subsequence (medium)
Solution: Longest Increasing Subsequence
Validate Binary Search Tree (medium)
Solution: Validate Binary Search Tree
Integer to English Words (hard)
Solution: Integer to English Words
Trapping Rain Water (hard)
Solution: Trapping Rain Water
First Missing Positive (hard)
Solution: First Missing Positive
Merge K Sorted Lists (hard)
Solution: Merge K Sorted Lists
Max Points on a Line (hard)
Solution: Max Points on a Line
Sudoku Solver (hard)
Solution: Sudoku Solver
Longest Valid Parentheses (hard)
Solution: Longest Valid Parentheses
Remove Nth Node From End of List (Medium)
Solution: Remove Nth Node From End of List
Median of Two Sorted Arrays (hard)
Solution: Median of Two Sorted Arrays
Constrained Subsequence Sum (hard)
Solution: Constrained Subsequence Sum
Solution: Meeting Rooms II

Problem Statement

Given an array of meeting intervals where intervals[i] = [starti, endi], return the minimum number of meeting rooms needed so that no meetings overlap.

Examples

Example 1:

  • Input: intervals = [[10, 15], [20, 25], [30, 35]]
  • Expected Output: 1
  • Justification: There are no overlapping intervals in the given list. So, only 1 meeting room is enough for all the meetings.

Example 2:

  • Input: intervals = [[10, 20], [15, 25], [24, 30], [5, 14], [22, 28], [1, 4], [27, 35]]
  • Expected Output: 3
  • Justification: Let's see how many meetings overlap at the same time:
    • [1, 4] starts first.
    • Then [5, 14] begins, no overlap yet.
    • [10, 20] overlaps with [5, 14]
    • [15, 25] overlaps with [10, 20]
    • [22, 28] overlaps with [15, 25]
    • [24, 30] overlaps with both [22, 28] and [15, 25]
    • [27, 35] overlaps with [24, 30]

Example 3:

  • Input: intervals = [[10, 20], [20, 30]]
  • Expected Output: 1
  • Justification: The end time of the first meeting is the same as the start time of the second meeting. So, one meeting can be scheduled right after the other in the same room.

Constraints:

  • 1 <= intervals.length <= 104
  • 0 <= starti < endi <= 106

Solution

To find the minimum number of meeting rooms required, we sort all the meetings by their start times. This allows us to always process meetings in chronological order. Then, we use a min-heap (priority queue) to track the end times of meetings that are currently using rooms. The heap helps us know which room becomes available the earliest.

For each meeting, we compare its start time with the earliest ending meeting (top of the heap). If the current meeting can reuse a room (its start time is greater than or equal to the earliest end time), we remove the ended meeting from the heap. Regardless, we then add the current meeting’s end time to the heap. At the end, the size of the heap tells us how many rooms were used at the same time — which is the minimum number of rooms required.

Step-by-Step Algorithm

  1. Check if there are no meetings:

    • If the input array intervals is empty, return 0 since no rooms are needed.
  2. Sort the meetings by their start times:

    • Use Arrays.sort() and sort all intervals based on the first element (start time).
  3. Create a min-heap (priority queue):

    • Initialize a PriorityQueue to keep track of ongoing meetings.
    • This heap will store the end times of meetings currently using a room.
    • The meeting that ends the earliest will always be at the top.
  4. Add the first meeting's end time to the heap:

    • This allocates the first room for the first meeting.
  5. Process the remaining meetings one by one:

    • For each meeting from the second onwards:
      • Compare its start time with the top of the heap (the earliest end time).
      • If the current meeting starts after or exactly when the earliest meeting ends:
        • Remove the top value from the heap (free up the room).
      • Add the current meeting’s end time to the heap (assign a room).
  6. Return the number of rooms used:

    • After processing all meetings, return the size of the heap.
    • This number represents the maximum number of rooms used at the same time, which is the minimum number of rooms required.

Algorithm Walkthrough

Step 1: Sort the intervals by start time

Sorted intervals:

[[1, 4], [5, 14], [10, 20], [15, 25], [22, 28], [24, 30], [27, 35]]

This ensures that we always process meetings in the order they begin.

Step 2: Initialize min-heap and add end time of the first meeting

  • We start by booking one room for the first meeting [1, 4].
  • We store its end time (4) in the heap to track when this room becomes free.
  • At this point, only one meeting is scheduled.

Heap status: [4]

Step 3: Process remaining meetings (from index 1 onward)

  • intervals[1] = [5, 14]

    • Start = 5
    • Earliest end in heap = 4
    • 5 >= 4 → ✅ Room is free
    • Remove 4 from heap (meeting [1, 4] has ended)
    • Add 14 to heap (allocate room for [5, 14])
    • Heap = [14]
  • intervals[2] = [10, 20]

    • Start = 10
    • Heap top = 14
    • 10 >= 14 → ❌ false
    • Room not free → Allocate new room
    • Add 20 to heap
    • Heap = [14, 20]
  • intervals[3] = [15, 25]

    • Start = 15
    • Heap top = 14
    • 15 >= 14 → ✅
    • Remove 14 from heap (free a room)
    • Add 25 to heap
    • Heap = [20, 25]
  • intervals[4] = [22, 28]

    • Start = 22
    • Heap top = 20
    • 22 >= 20 → ✅
    • Remove 20 from heap
    • Add 28 to heap
    • Heap = [25, 28]
  • intervals[5] = [24, 30]

    • Start = 24
    • Heap top = 25
    • 24 >= 25 → ❌
    • Need a new room
    • Add 30 to heap
    • Heap = [25, 28, 30]
  • intervals[6] = [27, 35]

    • Start = 27
    • Heap top = 25
    • 27 >= 25 → ✅
    • Remove 25 from heap
    • Add 35 to heap
    • Heap = [28, 30, 35]

Final Step: Return heap size

  • Final heap: [28, 30, 35]
  • Size = 3

This means 3 meeting rooms were used at peak overlap.

Code

Java

. . . .

Complexity Analysis

  • Time Complexity: The time complexity of our algorithm is , where (N) is the number of intervals. This is because we're sorting the intervals once and then using priority queues to process them.

  • Space Complexity: The space complexity is as we're storing all intervals in the worst case.

Meeting Rooms II (medium)
Delete Node in a BST (medium)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-Step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis